package com.scau.yangsuiyu.estore.dababases.entity;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author suiyu_yang
 * @description
 * @date 2019-06-04 19:20
 * @email suiyu_yang@163.com
 */
@Entity
public class Goods {
    private int id;
    private String goodsName;
    private double price;
    private Double promotionPrice;
    private Double deliveryPrice;
    private Integer sales;
    private Integer inventory;
    private String profile;
    private Timestamp createTime;
    private Timestamp lastUpdateTime;
    private boolean isDisable;
    private Shop shop;
    private Category category;
    private List<GoodsPhoto> goodsPhotoList;
    private List<Item> itemList;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "goods_name")
    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    @Basic
    @Column(name = "price")
    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Basic
    @Column(name = "promotion_price")
    public Double getPromotionPrice() {
        return promotionPrice;
    }

    public void setPromotionPrice(Double promotionPrice) {
        this.promotionPrice = promotionPrice;
    }

    @Basic
    @Column(name = "delivery_price")
    public Double getDeliveryPrice() {
        return deliveryPrice;
    }

    public void setDeliveryPrice(Double deliveryPrice) {
        this.deliveryPrice = deliveryPrice;
    }

    @Basic
    @Column(name = "sales")
    public Integer getSales() {
        return sales;
    }

    public void setSales(Integer sales) {
        this.sales = sales;
    }

    @Basic
    @Column(name = "inventory")
    public Integer getInventory() {
        return inventory;
    }

    public void setInventory(Integer inventory) {
        this.inventory = inventory;
    }

    @Basic
    @Column(name = "profile")
    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Basic
    @Column(name = "is_disable")
    public boolean isDisable() {
        return isDisable;
    }

    public void setDisable(boolean disable) {
        isDisable = disable;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "shop_id")
    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "category_id")
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }


    @OneToMany(fetch = FetchType.EAGER, mappedBy = "goods", cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    public List<GoodsPhoto> getGoodsPhotoList() {
        return goodsPhotoList;
    }

    public void setGoodsPhotoList(List<GoodsPhoto> goodsPhotoSet) {
        this.goodsPhotoList = goodsPhotoSet;
    }


    @OneToMany(fetch = FetchType.EAGER, mappedBy = "goods")
    public List<Item> getItemList() {
        return itemList;
    }

    public void setItemList(List<Item> itemList) {
        this.itemList = itemList;
    }


    @Basic
    @Column(name = "create_time")
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    @Basic
    @Column(name = "last_update_time")
    public Timestamp getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Timestamp lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }


    @Override
    public int hashCode() {
        return Objects.hash(id, goodsName, price, promotionPrice, deliveryPrice, sales, inventory, profile);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return id == goods.id &&
                Double.compare(goods.price, price) == 0 &&
                Objects.equals(goodsName, goods.goodsName) &&
                Objects.equals(promotionPrice, goods.promotionPrice) &&
                Objects.equals(deliveryPrice, goods.deliveryPrice) &&
                Objects.equals(sales, goods.sales) &&
                Objects.equals(inventory, goods.inventory) &&
                Objects.equals(profile, goods.profile);
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goodsName='" + goodsName + '\'' +
                ", price=" + price +
                ", promotionPrice=" + promotionPrice +
                ", deliveryPrice=" + deliveryPrice +
                ", sales=" + sales +
                ", inventory=" + inventory +
                ", profile='" + profile + '\'' +
                ", createTime=" + createTime +
                ", lastUpdateTime=" + lastUpdateTime +
                ", isDisable=" + isDisable +
                ", shop=" + shop +
                ", category=" + category +
                ", goodsPhotoSet=" + goodsPhotoList +
                '}';
    }

    public void removePhoto(GoodsPhoto goodsPhoto) {
        GoodsPhoto temp = null;
        for (GoodsPhoto gp : goodsPhotoList) {
            if (gp.getId() == goodsPhoto.getId()) {
                temp = gp;
                break;
            }
        }

        if (temp != null)
            goodsPhotoList.remove(temp);
    }
}
